define(['jquery', 'bootstrap', 'backend', 'table', 'form','colorpicker'], function ($, undefined, Backend, Table, Form,colorpicker) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activitymanage/activity/index' + location.search,
                    add_url: 'activitymanage/activity/add',
                    edit_url: 'activitymanage/activity/edit',
                    del_url: 'activitymanage/activity/del',
                    multi_url: 'activitymanage/activity/multi',
                    import_url: 'activitymanage/activity/import',
                    table: 'activity',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'store.name', title: __('Store_id'), operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'mincodeimage', title: __('推广码'), operate: false,events: Table.api.events.operate,formatter: Table.api.formatter.layerImages},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'starttime', title: __('Starttime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.dateHourMin},
                        {field: 'endtime', title: __('Endtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.dateHourMin},
                        {field: 'price', title: __('Price'), operate:false},
                        {field: 'trueprice', title: __('Trueprice'), operate:false},
                        {field: 'phone', title: __('Phone'), operate: false},
                        {field: 'readnums', title: __('Readnums'), operate:false},
                        {field: 'sharenums', title: __('分享数'), operate:false},
                        {field: 'signnums', title: __('报名数'), operate:false},
                        {field: 'manlimit', title: __('Manlimit'), operate:false},
                        {field: 'signmansswitch', title: __('Signmansswitch'), operate:false, searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'phoneswitch', title: __('Phoneswitch'), operate:false, searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'posterswitch', title: __('Posterswitch'), operate:false, searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'signswitch', title: __('Signswitch'), operate:false, searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'formswitch', title: __('Formswitch'), operate:false, searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'statusswitch', title: __('Statusswitch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'activitymanage/activity/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'activitymanage/activity/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'activitymanage/activity/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent(0);
        },
        edit: function () {

            Controller.api.bindevent(1);
        },
        api: {
            bindevent: function (isedit) {
                Form.api.bindevent($("form[role=form]"));
                var startimeChange = 0;
                if(isedit==1){
                    startimeChange = 1;
                    setTimeout(function () {
                        timeChange();
                    },1000)
                }else{
                    timeChange();
                }
                function timeChange(){
                    $("#c-starttime").on("dp.change", function (e) {
                        startimeChange += 1;
                        $('#c-endtime').data("DateTimePicker").minDate(e.date);
                    })

                    $("#c-endtime").on("dp.change", function (e) {
                        if(startimeChange!=1){
                            $('#c-starttime').data("DateTimePicker").maxDate(e.date);
                        }
                    });
                }

                $(document).on("change", "#c-formswitch", function(){
                    //开关切换后的回调事件
                    changeFormswitch($(this).val());
                });
                changeFormswitch($("#c-formswitch").val());
                function changeFormswitch(val){
                    if(val==0){
                        $(".formcont").show();
                    }else{
                        $(".formcont").hide();
                    }
                }
                //添加新表单
                $(".diyform").on("click",function(){
                    Fast.api.open("activitymanage/activity_form/add", "自定义表单", {
                        area: ['95%', '95%'],
                        shade: [0.8, '#393D49'],
                        shadeClose:true,
                        callback:function(data){

                        }
                    });
                })
            }
        }
    };
    return Controller;
});
